package com.example.viewanimatordemo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    // 常量：每屏显示的程序数量
    private static final int NUMBER_PER_SCREEN = 12;
    private final int NUM_APPS = 40;
    // 当前显示第几屏
    private int screenNo = 0;
    // 总屏数
    private int screenCount = 0;
    private ViewSwitcher switcher = null;
    // 布局翻译器
    private LayoutInflater inflater = null;
    BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            if ((screenNo == screenCount - 1) && (NUM_APPS % NUMBER_PER_SCREEN != 0))
                return NUM_APPS % NUMBER_PER_SCREEN;
            else
                return NUMBER_PER_SCREEN;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View screenView = convertView;
            if (convertView == null) {
                screenView = inflater.inflate(R.layout.labelicon, null);
            }
            ImageView img = screenView.findViewById(R.id.imageview);
            img.setImageResource(R.mipmap.ic_launcher);
            TextView txt = screenView.findViewById(R.id.textview);
            int n = (screenNo * NUMBER_PER_SCREEN + position) % NUM_APPS;
            String label = "App " + (n + 1);
            txt.setText(label);
            return screenView;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        inflater = LayoutInflater.from(this);
        screenCount = NUM_APPS % NUMBER_PER_SCREEN == 0 ? NUM_APPS / NUMBER_PER_SCREEN : NUM_APPS / NUMBER_PER_SCREEN + 1;

        switcher = findViewById(R.id.viewswitcher);
        switcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                View view = inflater.inflate(R.layout.slidelistview, null);
                return view;
            }
        });
        ((GridView) switcher.getNextView()).setAdapter(adapter);
        switcher.showNext();
    }

    // 监听器
    public void next(View v) {
        screenNo = (++screenNo) % screenCount;
        switcher.setInAnimation(this, android.R.anim.slide_in_left);
        switcher.setOutAnimation(this, android.R.anim.slide_out_right);
        ((GridView) switcher.getNextView()).setAdapter(adapter);
        switcher.showNext();
    }

    public void prev(View v) {
        screenNo = (--screenNo) % screenCount;
        if (screenNo < 0)
            screenNo += screenCount;
        switcher.setInAnimation(this, R.anim.slide_in_right);
        switcher.setOutAnimation(this, R.anim.slide_out_left);
        ((GridView) switcher.getNextView()).setAdapter(adapter);
        switcher.showPrevious();
    }
}
